<?php

class  wechat_login
{
    /**
     * @var string
     */
    protected $app_id = '';

    /**
     * @var string
     */
    protected $app_secret = '';

    /**
     * @var string
     */
    protected $redirect_url = '';

    /**
     * @var string 网站应用app id
     */
    protected $web_app_id = '';
    /**
     * @var string 网站应用app secret
     */
    protected $web_app_secret = '';

    function __construct($data)
    {
        $this->app_id = $data['app_id'];
        $this->app_secret = $data['app_secret'];
        $this->web_app_id = $data['web_app_id'];
        $this->web_app_secret = $data['web_app_secret'];
        $this->redirect_url = $data['redirect_url'];
    }

    /**
     * 微信登陆地址
     *
     * @param string $redirect_url 调整url
     * @param string $scope       请求用户信息的范围 snsapi_base|snsapi_userinfo|snsapi_login
     * @param string $env         production|testing
     *
     * @return string
     */
    public function create_login_url($redirect_url = '', $scope = 'snsapi_userinfo',$state='state')
    {

        if (!$redirect_url) {
            $redirect_url = $this->redirect_url;
        }
        $redirect_url = urlencode($redirect_url);

        if ($scope == 'snsapi_login') {
            $state .= 'browser';
            $loginUrl = 'https://open.weixin.qq.com/connect/qrconnect?appid='
                . $this->web_app_id . '&redirect_uri='
                . $redirect_url . '&response_type=code&scope='
                . $scope . '&state='
                . $state . '#wechat_redirect';
        } else {
            $loginUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='
                . $this->app_id . '&redirect_uri='
                . $redirect_url . '&response_type=code&scope='
                . $scope . '&state='
                . $state . '#wechat_redirect';
        }

        return $loginUrl;

    }


//    public function create_web_login

    /**
     * 获取微信授权access_token
     *
     * @param string $code code
     * @param string $state
     *
     * @return string
     */
    public function get_access_token($code, $state = '')
    {
        if (strpos($state, 'browser') !== false) {
            $app_id = $this->web_app_id;
            $app_secret = $this->web_app_secret;
        } else  {
            $app_id = $this->app_id;
            $app_secret = $this->app_secret;
        }
        $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='
            . $app_id . '&secret=' . $app_secret . '&code='
            . $code . '&grant_type=authorization_code';

        $token = $this->curl_get_content($token_url);

        return $token;
    }

    /**
     * 获取用户信息
     *
     * @param string $token  token
     * @param string $open_id openid
     *
     * @return array
     */
    public function get_user_info($token, $open_id)
    {
        $info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $token
            . '&openid=' . $open_id;
        $user = $this->curl_get_content($info_url);

        return $user;
    }

    /**
     * http get
     *
     * @param string $url url
     *
     * @return string
     */
    public function curl_get_content($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, 4);
        if (!curl_exec($ch)) {
            $data = curl_error($ch);
        } else {
            $data = curl_multi_getcontent($ch);
        }
        curl_close($ch);
        return $data;

    }
}